package com.angis.fx.activity.xtsz;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

import com.angis.fx.activity.Enforcement;
import com.angis.fx.activity.R;
import com.angis.fx.activity.jcdj.QuickTextActivity;
import com.angis.fx.activity.service.BusinessTypeService;
import com.angis.fx.activity.service.CKTypeService;
import com.angis.fx.activity.service.EcnomicTypeService;
import com.angis.fx.activity.service.GiszfjgService;
import com.angis.fx.db.XtszDBHelper;
import com.angis.fx.util.CommonUtil;

public class SystemSettingActivity extends Activity {

	private Button mQuickTextBt;
	private Button mCheckResultBt;
	private Button mGiszfjgBt;
	private Button mEcnomicTypeBt;
	private Button mBusinessTypeBt;
	private EditText mSecondEt;
	private EditText mMeterEt;
	private EditText mHostEt;
	private Button mSecondappBt;
	private Button mMeterappBt;
	private Button mHostappBt;
	private String mTempHost;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.systemsetting);
		mQuickTextBt = (Button) findViewById(R.id.ss_quicktext_bt);
		mCheckResultBt = (Button) findViewById(R.id.ss_checkupdate_bt);
		mGiszfjgBt = (Button) findViewById(R.id.ss_giszfjg_bt);
		mEcnomicTypeBt = (Button) findViewById(R.id.ss_ecnomictype_bt);
		mBusinessTypeBt = (Button) findViewById(R.id.ss_businesstype_bt);
		mSecondEt = (EditText) findViewById(R.id.ss_second_et);
		mSecondEt.setInputType(InputType.TYPE_CLASS_NUMBER);
		mMeterEt = (EditText) findViewById(R.id.ss_meter_et);
		mMeterEt.setInputType(InputType.TYPE_CLASS_NUMBER);
		mSecondappBt = (Button) findViewById(R.id.ss_secondapp_bt);
		mMeterappBt = (Button) findViewById(R.id.ss_meterapp_bt);
		mHostEt = (EditText) findViewById(R.id.ss_host_et);
		mHostappBt = (Button) findViewById(R.id.ss_hostapp_bt);
		XtszDBHelper lDBHelper = new XtszDBHelper(SystemSettingActivity.this);
		Cursor lCursor = lDBHelper.find();
		if(lCursor.moveToFirst()) {
			do {
				String lSecond = lCursor.getString(0);
				mSecondEt.setText(Integer.parseInt(lSecond)/1000+"");
				String lMeter = lCursor.getString(1);
				mMeterEt.setText(lMeter);
				String lHost = lCursor.getString(2);
				mHostEt.setText(lHost);
			} while (lCursor.moveToNext());
		}
		lCursor.close();
		lDBHelper.closeDB();
		mTempHost = mHostEt.getText().toString();
		mHostEt.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(!CommonUtil.validateIP(mHostEt.getText().toString()))
		        {
					Toast.makeText(SystemSettingActivity.this, "请输入正确的IP地址", Toast.LENGTH_SHORT).show();
		            mHostEt.setText(mTempHost);
		            mHostEt.requestFocus();
		        }
				return false;
			}
		});
		//快速文本
		mQuickTextBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent lIntent = new Intent();
				lIntent.putExtra("view", 1);
				lIntent.setClass(SystemSettingActivity.this, QuickTextActivity.class);
				SystemSettingActivity.this.startActivity(lIntent);
			}
		});
		//检查结果
		mCheckResultBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(SystemSettingActivity.this, "检查更新已运行!", Toast.LENGTH_SHORT).show();
				startService(new Intent(SystemSettingActivity.this, CKTypeService.class));
			}
		});
		//驻地检查
		mGiszfjgBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(SystemSettingActivity.this, "驻地信息更新已运行!", Toast.LENGTH_SHORT).show();
				startService(new Intent(SystemSettingActivity.this, GiszfjgService.class));
			}
		});
		//经营方式
		mBusinessTypeBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(SystemSettingActivity.this, "经营方式更新已运行!", Toast.LENGTH_SHORT).show();
				startService(new Intent(SystemSettingActivity.this, BusinessTypeService.class));
			}
		});
		//经济类型
		mEcnomicTypeBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(SystemSettingActivity.this, "经济类型更新已运行!", Toast.LENGTH_SHORT).show();
				startService(new Intent(SystemSettingActivity.this, EcnomicTypeService.class));
			}
		});
		mSecondappBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String lSecond = mSecondEt.getText().toString();
				if(lSecond.length() > 0) {
					try {
						XtszDBHelper lDBHelper = new XtszDBHelper(SystemSettingActivity.this);
						lDBHelper.update(Integer.parseInt(lSecond)*1000+"", null, null);
						lDBHelper.closeDB();
						Enforcement.GPSSecond = Integer.valueOf(lSecond)*1000;
						Toast.makeText(SystemSettingActivity.this, "已应用", Toast.LENGTH_SHORT).show();
					} catch (Exception e) {
						Toast.makeText(SystemSettingActivity.this, "请输入正确的单位秒", Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		mMeterappBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String lMeter = mMeterEt.getText().toString();
				if(lMeter.length() > 0) {
					try {
						XtszDBHelper lDBHelper = new XtszDBHelper(SystemSettingActivity.this);
						lDBHelper.update(null, lMeter, null);
						lDBHelper.closeDB();
						Enforcement.GPSExtent = Integer.valueOf(lMeter);
						Toast.makeText(SystemSettingActivity.this, "已应用", Toast.LENGTH_SHORT).show();
					} catch (Exception e) {
						Toast.makeText(SystemSettingActivity.this, "请输入正确的单位米", Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		mHostappBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String lHost = mHostEt.getText().toString();
				if(lHost.length() > 0) {
					try {
//						if(CommonUtil.validateIP(lHost)) {
							XtszDBHelper lDBHelper = new XtszDBHelper(SystemSettingActivity.this);
							lDBHelper.update(null, null, lHost);
							lDBHelper.closeDB();
							Enforcement.HOST = lHost;
							Toast.makeText(SystemSettingActivity.this, "已应用", Toast.LENGTH_SHORT).show();
//						}else {
//							Toast.makeText(SystemSettingActivity.this, "请输入正确的IP地址", Toast.LENGTH_SHORT).show();
//						}
					} catch (Exception e) {
						Toast.makeText(SystemSettingActivity.this, "请输入正确的IP地址", Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
	}

}
